package org.ttitfly.ttcore.service.account;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.ttitfly.ttcore.dao.account.ResourceDao;
import org.ttitfly.ttcore.entity.account.Resource;
import org.ttitfly.ttcore.entity.menu.SystemMenu;
import org.ttitfly.ttcore.so.account.ResourceSO;
import org.ttitfly.ttcore.util.tree.FlatTreeNode;
import org.ttitfly.ttcore.util.tree.StringComparator;
import org.ttitfly.ttcore.util.tree.TreeNode;
import org.ttitfly.ttcore.util.tree.TreeUtil;

//Spring Bean的标识.
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class ResourceManager {

	private static Logger logger = LoggerFactory.getLogger(ResourceManager.class);

	@javax.annotation.Resource
	private ResourceDao resourceDao;

	//-- Resource Manager --//
	@Transactional(readOnly = true)
	public Resource getResource(Long id) {
		return resourceDao.get(id);
	}

	public void saveResource(Resource entity) {
		resourceDao.save(entity);
	}
	
	public List<Resource> findAll(){
		return resourceDao.findAll();
	}

	/**
	 * 删除用户,如果尝试删除超级管理员将抛出异常.
	 */
	public void deleteResource(Long id) {
		resourceDao.delete(id);
	}

	/**
	 * 判断是否超级管理员.
	 */
	private boolean isSupervisor(Long id) {
		return id == 1;
	}

	/**
	 * 使用属性过滤条件查询用户.
	 */
	@Transactional(readOnly = true)
	public Page<Resource> searchResource(final Page<Resource> page, final List<PropertyFilter> filters) {
		return resourceDao.findPage(page, filters);
	}

	@Transactional(readOnly = true)
	public Resource findResourceByTitle(String title) {
		return resourceDao.findUniqueBy("title", title);
	}

	/**
	 * 检查用户名是否唯一.
	 *
	 * @return loginName在数据库中唯一或等于oldLoginName时返回true.
	 */
	@Transactional(readOnly = true)
	public boolean isLoginNameUnique(String newLoginName, String oldLoginName) {
		return resourceDao.isPropertyUnique("loginName", newLoginName, oldLoginName);
	}
	
	public int getSOResourceViewCount(ResourceSO so) {
        return resourceDao.getSOResourceViewCount(so);
    }

	public List<Resource> getSOResourceView(ResourceSO so) {
        return resourceDao.getSOResourceView(so);
    }
	
	public TreeNode buildTree(Long id){
		TreeNode result = null;
		List<FlatTreeNode> flatTreeNodeList = new ArrayList<FlatTreeNode>();
		//所有的孩子结点，包括孩子的孩子
		List<SystemMenu> poList = new ArrayList<SystemMenu>();
		List<Resource> list = resourceDao.getAll();
		for(Resource res : list){
//			TreeNode node = new TreeNode(""+res.getId(),res.getTitle());
//			poList.add(node);
			SystemMenu child = new SystemMenu();
			child.setId(res.getId());
			child.setTitle(res.getTitle());
			child.setLeaf(0);
			child.setParentId(0L);
			poList.add(child);
		}
		SystemMenu root = new SystemMenu();
		root.setId(id);
		root.setTitle("资源列表");
		root.setLeaf(0);
		root.setParentId(null);
		poList.add(root);//吧本身结点加入
		
		//add privilege list
		for(SystemMenu p : poList){
			flatTreeNodeList.add(new FlatTreeNode("" + p.getId(), p.getTitle(), "" + p.getParentId(), p));
		}
		result = TreeUtil.buildTreeFromFlatTreeNodeList(""+id, flatTreeNodeList, new StringComparator());
		return result;
	}
	
	public List<String> findAllCheckedNode(List<String> checkedNodeKeys){
		List<String> result = new ArrayList<String>();
		if(checkedNodeKeys != null){
//			TreeNode root = buildTree(0L);
			TreeNode root = new TreeNode("0","资源列表");
			//所有的孩子结点，包括孩子的孩子
			List<TreeNode> poList = new ArrayList<TreeNode>();
			List<Resource> list = resourceDao.getAll();
			for(Resource res : list){
				TreeNode node = new TreeNode(""+res.getId(),res.getTitle());
				poList.add(node);
			}
			root.setChildren(poList);
			result = TreeUtil.findAllCheckedNode(root, checkedNodeKeys);//所有子结点和选中的结点进行对比
		}
		return result;
	}

	public List<Resource> getByIds(List<Long> ids){
		if (ids != null && ids.size()>0) {
            return resourceDao.getByIds(ids);
        } else {
            return new ArrayList<Resource>(0);
        }
	}
	
}
